package ua.com.infinite.mobile.plains;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class NewsItem implements Parcelable {

	public static final String NEWS_TYPE = "type";
	public static final String NEWS_AVATAR = "avatar";
	public static final String NEWS_AUTHOR_ID = "author_id";
	public static final String NEWS_AUTHOR_NAME = "author_name";
	public static final String NEWS_POST_ID = "post_id";
	public static final String NEWS_POST_DATE = "post_date";
	public static final String NEWS_POST_TYTLE = "post_title";
	public static final String NEWS_POST_CONTENT = "post_content";
	public static final String NEWS_POST_IMG = "post_img";

	public int id;
	public String type;
	public String poster_icon;
	public String poster_name;
	public int poster_id;
	public String post_title;
	public String post_text;
	public String[] post_imgs;
	public String post_date;

	public NewsItem(int id, String type, String poster_icon, String poster_name, int poster_id, String post_title, String post_text,
			String[] post_imgs, String post_date) {
		this.id = id;
		this.type = type;
		this.poster_icon = poster_icon;
		this.poster_name = poster_name;
		this.poster_id = poster_id;
		this.post_title = post_title;
		this.post_text = post_text.replace("<br />", "\n");
		this.post_imgs = post_imgs;
		this.post_date = post_date;

	}

	public NewsItem(JSONObject object) {
		try {
			id = object.getInt(NEWS_POST_ID);
			type = object.getString(NEWS_TYPE);
			poster_icon = object.getString(NEWS_AVATAR);
			poster_name = object.getString(NEWS_AUTHOR_NAME);
			poster_id = object.getInt(NEWS_AUTHOR_ID);
			post_text = object.getString(NEWS_POST_CONTENT).replace("<br />", "\n");
			post_title = object.getString(NEWS_POST_TYTLE);
			post_date = object.getString(NEWS_POST_DATE);
			JSONArray imgs = object.getJSONArray(NEWS_POST_IMG);
			if (imgs.length() == 0) {
				post_imgs = new String[] {};
			} else {
				post_imgs = new String[imgs.length()];
				for (int i = 0; i < imgs.length(); i++) {
					post_imgs[i] = imgs.getString(i);
				}
			}
		} catch (JSONException e) {
			// TODO: handle exception
		}
	}

	// parcel

	public NewsItem(Parcel in) {
		id = in.readInt();
		poster_id = in.readInt();
		type = in.readString();
		poster_icon = in.readString();
		poster_name = in.readString();
		post_title = in.readString();
		post_text = in.readString();
		post_date = in.readString();
		int count = in.readInt();
		post_imgs = new String[count];
		for (int i = 0; i < count - 1; i++) {
			post_imgs[i] = in.readString();
		}
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeInt(poster_id);
		dest.writeString(type);
		dest.writeString(poster_icon);
		dest.writeString(poster_name);
		dest.writeString(post_title);
		dest.writeString(post_text);
		dest.writeString(post_date);
		dest.writeInt(post_imgs.length);
		for (int i = 0; i < post_imgs.length - 1; i++) {
			dest.writeString(post_imgs[i]);
		}
	}

	public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
		public NewsItem createFromParcel(Parcel in) {
			return new NewsItem(in);
		}

		public NewsItem[] newArray(int size) {
			return new NewsItem[size];
		}
	};

	@Override
	public int describeContents() {

		return 0;
	}

}
